/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.util.review;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class Aspects implements Serializable {

	private static final long serialVersionUID = 7858423130226414598L;
	
	/**
	 * Gets all categories, where are available in aspects
	 * @param aspects search categories in this aspects
	 * @param emptyCategoryIsCategory <code>true</code> if empty catalogs,
	 * that are signalizes by <code>""</code> or <code>null</code>, should be 
	 * added to categories.
	 * @return all available categories
	 */
	public static String[] getCategories(IdList<Aspect> aspects, 
			boolean emptyCategoryIsCategory) {
		if(aspects == null)
			return null;
		
		List<String> categoryList = new LinkedList<String>();
		Aspect aspect;

		for(int i = 0 ; i < aspects.size() ; i++) {
			aspect = aspects.get(i);
			if(aspect == null) {
				continue;
			} else if(aspect.getCategory() == null || 
						aspect.getCategory().equals("")) {
				if(!categoryList.contains("")) {
					if(emptyCategoryIsCategory)
						categoryList.add("");
				}
				continue;
			} else 	if(!categoryList.contains(aspect.getCategory())) {
				categoryList.add(aspect.getCategory());
			}
		}
		
		String[] cArray = new String[categoryList.size()];
		
		categoryList.toArray(cArray);
		
		return cArray;
	}
	
	/**
	 * Gets all categories, where are available in aspects
	 * @param aspects search categories in this aspects
	 * @param emptyCategoryisCategory <code>true</code> if empty catalogs,
	 * that are signalize by <code>""</code> or <code>null</code>, should be 
	 * added to categories.
	 * @return all available categories
	 */
	public static String[] getCategories(Aspect[] aspects, 
			boolean emptyCategoryIsCategory) {
		if(aspects == null)
			return null;
		
		List<String> categoryList = new LinkedList<String>();
		Aspect aspect;

		for(int i = 0 ; i < aspects.length ; i++) {
			aspect = aspects[i];
			if(aspect == null) {
				continue;
			} else if(aspect.getCategory() == null || 
						aspect.getCategory().equals("")) {
				if(!categoryList.contains("")) {
					if(emptyCategoryIsCategory)
						categoryList.add("");
				}
				continue;
			} else 	if(!categoryList.contains(aspect.getCategory())) {
				categoryList.add(aspect.getCategory());
			}
		}
		
		String[] cArray = new String[categoryList.size()];
		
		categoryList.toArray(cArray);
		
		return cArray;
	}
	
	/**
	 * Gets all aspects in a category
	 * @param aspects search category-aspects in this given aspects
	 * @param category category to search for.
	 * @return aspects in category
	 */
	public static Aspect[] getCategoryAspects(Aspect[] aspects, 
			String category) {
		
		if(aspects == null)
			return null;
		
		IdList<Aspect> categoryAspects = new IdLinkedList<Aspect>();
		Aspect aspect;
		
		if(category == null || category.equals("")) {
			for(int i = 0 ; i < aspects.length ; i++) {
				aspect = aspects[i];
				if(aspect == null)
					continue;
				if(aspect.getCategory() == null || 
						aspect.getCategory().equals("")) {
					categoryAspects.add(aspect);
				}
			}
		} else {
			for(int i = 0 ; i < aspects.length ; i++) {
				aspect = aspects[i];
				if(aspect == null)
					continue;
				if(category.equals(aspect.getCategory())) {
					categoryAspects.add(aspect);
				}
			}
		}
		
		Aspect[] aArray = new Aspect[categoryAspects.size()];
		
		categoryAspects.toArray(aArray);
		
		return aArray;
	}
	
	/**
	 * Gets all aspects in a category
	 * @param aspects search category-aspects in this given aspects
	 * @param category category to search for.
	 * @return aspects in category
	 */
	public static Aspect[] getCategoryAspects(IdList<Aspect> aspects, 
			String category) {
		
		if(aspects == null)
			return null;
		
		IdList<Aspect> categoryAspects = new IdLinkedList<Aspect>();
		Aspect aspect;
		
		if(category == null || category.equals("")) {
			for(int i = 0 ; i < aspects.size() ; i++) {
				aspect = aspects.get(i);
				if(aspect == null)
					continue;
				if(aspect.getCategory() == null || 
						aspect.getCategory().equals("")) {
					categoryAspects.add(aspect);
				}
			}
		} else {
			for(int i = 0 ; i < aspects.size() ; i++) {
				aspect = aspects.get(i);
				if(aspect == null)
					continue;
				if(category.equals(aspect.getCategory())) {
					categoryAspects.add(aspect);
				}
			}
		}
		
		Aspect[] aArray = new Aspect[categoryAspects.size()];
		
		categoryAspects.toArray(aArray);
		
		return aArray;
	}
}
